#! /bin/sh

cat <<EOF
`echo "(1) basecenter"`
`echo "(2) maindatacenter"`
`echo "(3) operatecenter"`
`echo "(4) businessapp"`
`echo "(0) quit"`
EOF

deploy_image() {
  echo "Remove existing image of ${1}..."
  docker rmi "0.0.0.0:8088/businesscenter/${1}:uat"
  docker rmi "abc.cn-northwest-1.amazonaws.com.cn/${2}_service:latest"

  echo "Update image of ${1}..."
  docker load -i "${1}.jar"
  docker tag "0.0.0.0:8088/businesscenter/${1}:uat" "abc.cn-northwest-1.amazonaws.com.cn/${2}_service:latest"
  docker push "abc.cn-northwest-1.amazonaws.com.cn/${2}_service:latest"

  kubectl get deployment -n surrounding-uat

  echo "Remove deployment of ${1}..."
  kubectl delete deployment "${1}-deployment" -n surrounding-uat
  
  echo "Apply deployment configuration of ${1}..."
  kubectl apply -f "~/surrounding_uat/${1}Deployment.yaml"

  echo "Deployment status"
  kubectl get deployment -n surrounding-uat
  echo "Pod status"
  kubectl get pod -n surrounding-uat
}

read -p "Please input service number": num
case $num in
  (1)
  deploy_image basecenter basecenter
  deploy_image maindatacenter maindata
  deploy_image operatecenter operate
  deploy_image businessapp businessapp
  ;;
  (2)
  deploy_image maindatacenter maindata
  deploy_image operatecenter operate
  deploy_image businessapp businessapp
  ;;
  (3)
  deploy_image operatecenter operate
  deploy_image businessapp businessapp
  ;;
  (4)
  deploy_image businessapp businessapp
  ;;
  (0)
  exit 0
esac
